﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessSecurity;

namespace DataSecurity.ADO.SqlServer.DataContext
{
    /// <summary>
    /// User Mapper
    /// </summary>
    public static class Mapper
    {
        /// <summary>
        /// To the data table.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            var props =
                TypeDescriptor.GetProperties(typeof(T));
            var table = new DataTable();
            for (var i = 0; i < props.Count; i++)
            {
                var prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            var values = new object[props.Count];
            foreach (T item in data)
            {
                for (var i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }
        /// <summary>
        /// To the data context.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static PermissionInSiteDc ToDataContext(this PermissionInSite obj)
        { 
            return new PermissionInSiteDc
                {
                    SiteId = obj.SiteId,
                    PermissionId = obj.PermissionId
                };
        }
        /// <summary>
        /// To the data context.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static List<PermissionInSiteDc> ToDataContext(List<PermissionInSite> objs)
        {
            return objs == null ? null : objs.Select(ToDataContext).ToList();
        }
        /// <summary>
        /// To the data context.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static RoleInSiteDc ToDataContext(RoleInSite obj)
        {
            return new RoleInSiteDc
            {
                RoleId = obj.RoleId,
                SiteId = obj.SiteId,
                PermissionId = obj.PermissionId
            };
        }
        /// <summary>
        /// To the data context.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static List<RoleInSiteDc> ToDataContext(List<RoleInSite> objs)
        {
            return objs == null ? null : objs.Select(ToDataContext).ToList();
        }
    }
}
